package com.example.templateapp.util

import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

object PermissionUtil {
    fun requestPermission(
        context: AppCompatActivity,
        permissions: Array<String>,
        reqCode: Int
    ): Boolean {
        var isGranted = false
        val denyPermissions = mutableListOf<String>()
        for (permission in permissions) {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                denyPermissions.add(permission)
            }
        }

        if (denyPermissions.isNotEmpty()) {
            isGranted = false
            ActivityCompat.requestPermissions(context, denyPermissions.toTypedArray(), reqCode)
        } else {
            isGranted = true
        }

        return isGranted
    }
}